const app = getApp()
Page({
    data: {
        isLogin: wx.getStorageSync('openid'),
        userInfo: {
            avatar: app.getImageUrl(wx.getStorageSync('userInfo').avatar),
            nickName: wx.getStorageSync('userInfo').nickName,
        },
        defaultAvatarUrl: `http://localhost:9000/wish-go/1664190089548007100.png`,
        cellList: [],
    },
    onLoad() {
        this.setData({
            isLogin: wx.getStorageSync('openid'),
            cellList: [{
                title: '我的心愿',
                dataUrl: `/pages/index/index?openid=${wx.getStorageSync('openid')}`,
                login: wx.getStorageSync('openid')
            }, {
                title: '填报心愿/建议',
                dataUrl: `/pages/index/add-wish/index?type=1`,
                login: wx.getStorageSync('openid')
            }, {
                title: '社区活动',
                dataUrl: `/pages/activity/activity`,
                login: true
            }, {
                title: '退出登录',
                dataUrl: `/pages/index/index`,
                login: wx.getStorageSync('openid'),
                logout: true
            }]
        });
    },
    onShow() {
        this.getTabBar().init();
    },
    goToPage(e) {
      const url = e.target.dataset.url;
      const logout = e.target.dataset.type;
      if (logout) {
          // 清空缓存
          wx.clearStorageSync();
          this.setData({
              isLogin: false
          });
      }
      wx.reLaunch({
        url: `${url}`,
      });
    },
    /**
     * 前往用户界面
     */
    goUserInfo() {
        wx.navigateTo({ url: '/pages/user/user-info/index' });
    },

    /**
     * 登录
     */
    login() {
        app.httpLogin().then(() => {
            // 刷新当前页面
            this.onLoad();
        });
    }
})